package com.micro.service.auth.server.redis;

import org.apache.commons.lang.StringUtils;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

/**
 * @author xiucai
 * @description redis管理
 * @date 2018/12/17 0017 17:16
 */
@Service
public class RedisManagerImpl implements RedisManager {


    @Resource(name = "redisNewSysTemplate")
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public void saveVcode(String mobile, String code, int expireTime, TimeUnit unit) {
        try {
            stringRedisTemplate.opsForValue().set(mobile,code,expireTime,unit);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public Object getVcode(String mobile) {
        if(mobile == null){
            return null;
        }
        String result = stringRedisTemplate.opsForValue().get(mobile);
        return result;
    }

    @Override
    public void removeVcode(String mobile) {
        if(mobile == null){
            return;
        }
        stringRedisTemplate.delete(mobile);
    }

    @Override
    public long incr(String redisKey) {
        if(StringUtils.isBlank(redisKey)){
            return 0;
        }
        return stringRedisTemplate.opsForValue().increment(redisKey);
    }

    @Override
    public void expire(String key, int time) {
        if(key == null){
            return;
        }
        stringRedisTemplate.expire(key,time,TimeUnit.SECONDS);
    }


}
